package com.meichis.yndms.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.meichis.mcsappframework.adapter.recyclerview.CommonAdapter;
import com.meichis.mcsappframework.adapter.recyclerview.base.ViewHolder;
import com.meichis.mcsappframework.utils.UniversalInterface;
import com.meichis.mcsappframework.widget.ZoomImageView;
import com.meichis.yndms.R;
import com.meichis.yndms.common.PictureUtils;
import com.meichis.yndms.model.Picture;
import com.meichis.yndms.room.AppDatabase;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.io.File;
import java.util.List;

/**
 * Created by chenhaifei on 2016/8/5.
 */
public class ImageAdapter extends CommonAdapter<Picture> {

    private DisplayImageOptions options;

    private UniversalInterface<Void, Integer> onDeleteListener;
    private String[] imageUrls;
    private List<Picture> mDatas;

    public void setonDeleteListener(UniversalInterface<Void, Integer> onDeleteListener) {
        this.onDeleteListener = onDeleteListener;
    }

    public ImageAdapter(Context context, int layoutId, List<Picture> datas) {
        super(context, layoutId, datas);
        mDatas = datas;
        options = new DisplayImageOptions.Builder().showImageOnLoading(R.mipmap.emptyphoto).showImageForEmptyUri(R.mipmap.emptyphoto).showImageOnFail(R.mipmap.emptyphoto).cacheInMemory(true)
                .bitmapConfig(Bitmap.Config.RGB_565).cacheOnDisk(true).considerExifParams(true).build();

    }

    @Override
    protected void convert(ViewHolder holder, final Picture picture, final int position) {

        imageUrls = new String[mDatas.size()];
        Picture p;
        for (int i = 0; i < mDatas.size(); i++) {
            p = AppDatabase.getInstance().pictureDao().find(mDatas.get(i).getGuid());
            if (mDatas.get(i).getFilePath().length() == 0 && p != null) {
                imageUrls[i] = p.getUILFilePath();
            } else {
                if (!TextUtils.isEmpty(mDatas.get(i).getFilePath())) {
                    File file = new File(picture.getFilePath());
                    if (file.exists()) {
                        imageUrls[i] = mDatas.get(i).getUILFilePath();
                    } else {
                        mDatas.get(i).setFilePath("");
                        imageUrls[i] = mDatas.get(i).getUILFilePath();
                    }
                } else {
                    imageUrls[i] = mDatas.get(i).getUILFilePath();
                }
            }
        }
        ZoomImageView view = holder.getView(R.id.iv_photo);
        view.setScaleType(ImageView.ScaleType.CENTER_CROP);
        view.displayImage(imageUrls, position, options);
        if (onDeleteListener != null) {
            if (holder.IsContainView(R.id.iv_cancel)) {
                holder.setOnClickListener(R.id.iv_cancel, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PictureUtils.deletePicture(picture);
                        getDatas().remove(position);
                        onDeleteListener.todo(position);
                        notifyDataSetChanged();
                    }
                });
            }
        } else {
            if (holder.IsContainView(R.id.iv_cancel)) {
                holder.getView(R.id.iv_cancel).setVisibility(View.INVISIBLE);
            }
        }
        if (TextUtils.isEmpty(picture.getFilePath())) {
            holder.getView(R.id.iv_cancel).setVisibility(View.INVISIBLE);
        }

    }


}
